package cn.wanho.service.impl;

import cn.wanho.dto.ExamClassStage;
import cn.wanho.mapper.ExamClassMapper;
import cn.wanho.mapper.ExamClassStageMapper;
import cn.wanho.mapper.ExamUserMapper;
import cn.wanho.rvo.ResultEnum;
import cn.wanho.rvo.ResultVO;
import cn.wanho.rvo.StudentRVO;
import cn.wanho.service.user.ClassService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author Zxy
 * @Date 2022/8/5 16:55
 * @Version 1.0
 */
@Service
public class ClassServiceImpl implements ClassService {

    @Resource
    private ExamClassStageMapper examClassStageMapper;

    @Resource
    private ExamUserMapper examUserMapper;

    @Resource
    private ExamClassMapper examClassMapper;

    /**
     * @author zxy
     * @date 2022/8/5
     * 将老师阶段试卷绑定 到班级
     * 班级 阶段 老师 开始 结束
     */
    @Override
    public ResultVO createClassStage(Integer classId, Integer stageId, Integer teacherId, Date startTime, Date endTime, Integer paperId, HttpServletRequest request) {
        //判断权限
        //获取创建人父id
//        Long userId = Long.valueOf((String) request.getAttribute("userId"));
        Integer userId = (Integer) request.getAttribute("userId");
        Integer flag = examUserMapper.judge(Long.valueOf(userId));
        Long paperIdL = Long.valueOf(paperId);
        //创建对象
        ExamClassStage cs = new ExamClassStage(null, classId, stageId, teacherId, startTime, endTime, paperIdL, null, null, null);
        if (flag == 1 || flag == 2) {
            //判断classId存在
            if (examClassStageMapper.existClassId(classId) == null) {
                return ResultVO.faiure(ResultEnum.ADD_CLASSID_FAILURE.getResCode(), ResultEnum.ADD_CLASSID_FAILURE.getResMsg());
            }
            //stageId存在
            if (examClassStageMapper.existStageId(stageId) == null) {
                return ResultVO.faiure(ResultEnum.ADD_STAGEID_FAILURE.getResCode(), ResultEnum.ADD_STAGEID_FAILURE.getResMsg());

            }
            //teacherId存在
            if (examClassStageMapper.existTeacherId(teacherId) == null) {
                return ResultVO.faiure(ResultEnum.ADD_TEACHER_FAILURE.getResCode(), ResultEnum.ADD_TEACHER_FAILURE.getResMsg());
            }
            //paperIdL存在
            if (examClassStageMapper.existpaperIdL(paperIdL) == null) {
                return ResultVO.faiure(ResultEnum.ADD_PAPERID_FAILURE.getResCode(), ResultEnum.ADD_PAPERID_FAILURE.getResMsg());
            }
            //重复判断
            if (examClassStageMapper.existRepeat(classId, stageId, teacherId, paperId).size() != 0) {
                return ResultVO.faiure(ResultEnum.REPEAT_FAILURE.getResCode(), ResultEnum.REPEAT_FAILURE.getResMsg());
            }
            //通过-存入
            int i = examClassStageMapper.insertSelective(cs);
            if (i == 1) {
                //存入成功
                return ResultVO.success(ResultEnum.ADD_CLASSSTAGE_SUCCESS.getResCode(), ResultEnum.ADD_CLASSSTAGE_SUCCESS.getResMsg());
            } else {
                return ResultVO.faiure(ResultEnum.ADD_CLASSSTAGE_FAILURE.getResCode(), ResultEnum.ADD_CLASSSTAGE_FAILURE.getResMsg());
            }
        }
        return ResultVO.faiure(ResultEnum.INSUFFICIENT_PERMISSIONS.getResCode(), ResultEnum.INSUFFICIENT_PERMISSIONS.getResMsg());
    }

    /**
     * @author zxy
     * @date 2022/8/6
     * 将学生绑定到班级
     */
    @Override
    public ResultVO createClassStudent(Integer classId, Integer studentId, HttpServletRequest request) {
        //判断权限
        //获取创建人父id
//        Long userId = Long.valueOf((String) request.getAttribute("userId"));
        Integer userId = (Integer) request.getAttribute("userId");
        Integer flag = examUserMapper.judge(Long.valueOf(userId));
        if (flag == 1 || flag == 2) {
            //班级存在
            if (examClassStageMapper.existClassId(classId) == null) {
                return ResultVO.faiure(ResultEnum.ADD_CLASSID_FAILURE.getResCode(), ResultEnum.ADD_CLASSID_FAILURE.getResMsg());
            }
            //学生存在
            if (examClassStageMapper.existStudent(studentId) == null) {
                return ResultVO.faiure(ResultEnum.ADD_STUDENTPAPERID_FAILURE.getResCode(), ResultEnum.ADD_STUDENTPAPERID_FAILURE.getResMsg());
            }
            //重复判断
            if (examClassStageMapper.existRepeat2(classId, studentId).size() != 0) {
                return ResultVO.faiure(ResultEnum.REPEAT_FAILURE.getResCode(), ResultEnum.REPEAT_FAILURE.getResMsg());
            }
            Integer classStudent = examClassStageMapper.createClassStudent(classId, studentId);
            if (classStudent == 1) {
                return ResultVO.success(ResultEnum.ADD_CLASSSTUDENT_SUCCESS.getResCode(), ResultEnum.ADD_CLASSSTUDENT_SUCCESS.getResMsg());
            } else {
                return ResultVO.faiure(ResultEnum.ADD_CLASSSTUDENT_FAILURE.getResCode(), ResultEnum.ADD_CLASSSTUDENT_FAILURE.getResMsg());
            }
        }
        return null;
    }



    @Override
    public ResultVO createClass(String className,Integer id) {
        Integer role = examUserMapper.selectRole(id);
        try {
            if (role == 1 || role == 2){
                int i = examClassMapper.createClass(className);
                if (i > 0){
                    return ResultVO.success(ResultEnum.CREATE_CLASS_SUCCESS.getResCode(),ResultEnum.CREATE_CLASS_SUCCESS.getResMsg());
                }else{
                    return ResultVO.faiure(ResultEnum.CREATE_CLASS_FAILURE.getResCode(),ResultEnum.CREATE_CLASS_FAILURE.getResMsg());
                }
            }else {
                return ResultVO.faiure(ResultEnum.NO_MATCH_INFORMATION.getResCode(), ResultEnum.NO_MATCH_INFORMATION.getResMsg());
            }
        }catch(Exception e){
            return ResultVO.faiure(ResultEnum.CREATE_CLASS_FAILURE.getResCode(),ResultEnum.CREATE_CLASS_FAILURE.getResMsg());
        }
    }

    @Override
    public ResultVO createStage(String stageName, Integer id) {
        Integer role = examUserMapper.selectRole(id);
        try {
            if (role == 1 || role == 2){
                int i = examClassMapper.createStage(stageName);
                if (i > 0){
                    return ResultVO.success(ResultEnum.CREATE_STAGE_SUCCESS.getResCode(), ResultEnum.CREATE_STAGE_SUCCESS.getResMsg());
                }else{
                    return ResultVO.faiure(ResultEnum.CREATE_STAGE_FAILURE.getResCode(), ResultEnum.CREATE_STAGE_FAILURE.getResMsg());
                }
            }else {
                return ResultVO.faiure(ResultEnum.NO_MATCH_INFORMATION.getResCode(), ResultEnum.NO_MATCH_INFORMATION.getResMsg());
            }
        }catch(Exception e){
            return ResultVO.faiure(ResultEnum.CREATE_STAGE_FAILURE.getResCode(), ResultEnum.CREATE_STAGE_FAILURE.getResMsg());
        }
    }

    @Override
    public ResultVO queryAllStudentScore(String className,
                                         String teacherName,
                                         String studentName,
                                         String stageName) {
        List<StudentRVO> list = examClassMapper.queryAllStudentScore(className, teacherName, studentName, stageName);
        if (list.isEmpty()){
            return ResultVO.faiure(ResultEnum.QUERY_STUDENT_SCORE_FAILURE.getResCode(), ResultEnum.QUERY_STUDENT_SCORE_FAILURE.getResMsg());
        }else{
            HashMap<String, Object> map = new HashMap<>();
            map.put("学生考试信息",list);
            return ResultVO.success(ResultEnum.QUERY_STUDENT_SCORE_SUCCESS.getResCode(), ResultEnum.QUERY_STUDENT_SCORE_SUCCESS.getResMsg(),map);
        }
    }
}
